<?php
/**
 * @description : all support methods list on all works.
 * @class-name : GearmandSupportMethodsList();
 * @rule : 1).when add a new function or static method,you'll know the function name at first.
 *         2).use this format write a function<method> : eg: function-name_fn($job);
 * @author : stephen.mo <stephen.mo@achievo.com>
 * @date : 2010-03-29
 */

require_once ( 'TVB/Entity/MemberBrowseHistoryEntity.php' ) ;
require_once ( 'TVB/Entity/MemberFavourEntity.php' ) ;

class GearmandSupportMethodsList {

	static public function writeHistoryEntity($job) {
		error_log ( __FUNCTION__ ) ;
		$workload= $job->workload();
		$object = json_decode($workload,true);
		//var_dump ( $object ) ;
		$uid = $object['member_id'] ;
		//$browse_history_entity = new MemberBrowseHistoryEntity( $uid ) ;
		//$browse_history_entity->insert ( $object ) ;
		return true ;
	}

	/**
	 * @description : reader data from jobServer.
	 * @action : get & anaylse the datas and write it to logs file.
	 * @author : stephen.mo <stephen.mo@achievo.com>
	 * @date : 2010-03-29
	 */
	static public function mstatWirteLogFromJobServers($job) {
		$workload= $job->workload();
		$object = json_decode($workload,true) ;
		
		$uid = $object['member_id'] ;

		if (!isset($object['type'])) return false;
		if ( $object['type'] == 'favour' ) {
			// do nothing
			return true ;
			$sample_obj = array ('datetime' => '' ,'ui_lang' => '', 'client_ip' => '', 'country_code' => '' , 'url' => '' ,'video_id' => '', 'programme_id' => '', 'user_agent' => '' ) ;
			$entity_record = array_intersect_key( $object, $sample_obj ) ;
			//$browse_history_entity = new MemberBrowseHistoryEntity( $uid ) ;
			$browse_history_entity = MemberBrowseHistoryEntity::getInstance( $uid ) ;
			$browse_history_entity->insert ( $entity_record ) ;
			return true ;
		}

		$const_name = publicGearmandConfig::LOG_FILE_PREFIX.strtolower($object['type']);
		$log_file_path = publicGearmandConfig::LOG_FILE_PATH.strtolower($object['type'])."/".date('Y').'/'.date('m').'/'.date('d').'/';
		if (!is_dir($log_file_path)) {
			exec("mkdir -p ".$log_file_path);
		}

		$log_file_prefix = publicGearmandConfig::$$const_name;
		//$time_of_current_min = self::timeOfSlicing($object['datetime']);
		$time_of_current_min = self::timeOfSlicing(date('r'));

		if (empty($time_of_current_min)) return false;

		//intergration workload arrays.
		$workload_array = array();
		foreach($object as $obj=>$val) {
			if ($obj=='type') continue;
			if ($obj=='datetime') $val = date('Y-m-d H:i:s',date('U',strtotime($val)));
			$workload_array["$obj"]=$val;
		}

		//call logger write and write the data to xxx.log.
		$logfile = $log_file_path.$log_file_prefix.$time_of_current_min;
		$log_entity = array('type'=>$object['type'],'file'=>$logfile,'data'=>$workload_array);
		return GearmandWriteLog::write($log_entity);
	}


	/**
	 * @description : do with the Time Slicing
	 * @action : analyse the datetime and return the log file name.
	 * @author : stephen.mo <stephen.mo@achievo.com>
	 * @date : 2010-04-08
	 */
	static public function timeOfSlicing($datetime) {
		if (empty($datetime)) return false;
		$gmt_time = date('U',strtotime($datetime));
		$current_time = date('i',$gmt_time);
		$time_scling = array();
		$time_scling = publicGearmandConfig::timeOfScling();
		$default_suffix = date('Y_m_d_H_');
		$result = '';
		foreach($time_scling as $key=>$value) {
			if ($current_time>=$value['min'] && $current_time<=$value['max']) {
				$result = $default_suffix.$key.".log";
				break;
			}
		}

		return (empty($result)) ? false : $result;
	}


	/**
	 * @description : read a job from the Jobserver .
	 *                and ask the worker must be analyse the log file now.
	 * @action : begin to analyse the log file or run scripts.
	 * @author : stephen.mo <stephen.mo@achievo.com>
	 * @date : 2010-04-08.
	 */
	static public function mstatWirteJobFromJobServers($job) {
		//error_log ( '['.date('Y-m-d H:i:s').'] Starting...' ) ;
		$workload = $job->workload();
		$object = json_decode($workload,true);
		//error_log ( '['.date('Y-m-d H:i:s').'] '.$object[0] ) ;
		if (empty($object)) return false;
		$bin_path = publicGearmandConfig::GEARMAND_GEN_BIN_SCRIPT_PATH;

		//foreach($object as $key=>$value) {
			if (empty($object['exec_job'])) return false;
			$file_name = 'get_'.$object['exec_job'].".sh";
			$file_arg = $object['exec_time'];
			$full_path = $bin_path.$object['exec_job'].'/bin/'.$file_name.' "'.$file_arg.'"';
			if (is_file($full_path) && is_executable($full_path)) {
				exec($full_path);                
			} else {
				//write the execute log.
			}
		//}
		//error_log ( '['.date('Y-m-d H:i:s').'] Ending...' ) ;
		return true;
	}
}

?>
